package com.fs.system.controller;

import com.fs.common.constant.Constants;
import com.fs.common.core.pojo.SysMenu;
import com.fs.common.core.pojo.SysUser;
import com.fs.common.core.vo.AjaxResult;
import com.fs.common.core.vo.LoginBody;
import com.fs.common.core.vo.LoginUser;
import com.fs.common.util.ServletUtils;
import com.fs.system.service.ISysLoginService;
import com.fs.system.service.ISysMenuService;
import com.fs.system.service.impl.SysPermissionService;
import com.fs.system.service.impl.TokenService;
import com.fs.system.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author suke
 * @version 1.0
 * @title SysLoginController
 * @description  处理登录相关的Controller
 * @create 2024/7/24 9:21
 */
@RestController
public class SysLoginController {
    @Autowired
    private ISysLoginService sysLoginService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private ISysMenuService sysMenuService;


    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody){
        String token = sysLoginService.login(loginBody.getUsername(),loginBody.getPassword(),loginBody.getCode(),loginBody.getUuid());
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put(Constants.TOKEN,token);
        return ajaxResult;
    }

    @GetMapping("/getInfo")
    public AjaxResult getInfo(){
    AjaxResult ajaxResult=new AjaxResult();
        SysUser user= SecurityUtils.getLoginUser().getUser();
        //角色信息
        Set<String> roles = sysPermissionService.getRolePermission(user);
        //权限表达式信息
        Set<String> menuPermissions = sysPermissionService.getMenuPermission(user);

        ajaxResult.put("user",user);
        ajaxResult.put("roles",roles);
        ajaxResult.put("permissions",menuPermissions);
        return ajaxResult;
    }

    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(sysMenuService.buildMenus(menus));
    }
}
